import React, { Component } from 'react';
import { Form, Row, Col } from 'antd';
import styles from '../../../index.less';
import StaticEditTable from '../Common/ProductStaticEditTable';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class JigsawComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      formData: props.formData,
      sId: props.sId, /* 业务数据Id */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      workOrderJigSawSlaveConfig: {}, /* 拼版配置 */
      headerConfig: [], /* 表头配置 */
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig,
      sId,
      formData,
      componentName,
      sActiveId,
      sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (masterConfig.sId !== '') {
      const workOrderJigSawSlaveConfig = formData.filter(item => (item.sTbName.toLowerCase() === 'mftproductcontrol'))[0];
      /* 主表配置 */
      if (sId !== undefined && sId !== '') {
        const subjectId = masterConfig.sId;
        /* 表Id */
        const value = {
          sSqlCondition: {
            sParentId: sId,
          },
        };
        const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${subjectId}?sModelsId=${sActiveId}`;
        dispatch({
          type: 'content/getUrlPostData',
          payload: {
            url: dataUrl,
            sTabId,
            componentName,
            value,
            formRoute: '/processcard',
            returnType: 'data',
          },
        });
      }
      /* 获取表头 */
      const headerConfig = [];
      for (const child of workOrderJigSawSlaveConfig.gdsconfigformslave) {
        if (child.sName !== '' && child.bVisible && child.showName !== '') {
          headerConfig.push({
            title: child.showName,
            dataIndex: child.sName,
            width: child.iFitWidth,
          });
        }
      }
      this.setState({
        headerConfig,
        workOrderJigSawSlaveConfig,
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const { disabled, content } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId,
      returnType,
      loading,
      componentName,
    } = dataList;
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        if (componentName === 'workorderjigsaw') {
          this.setState({ disabled });
        }
      }
    }
    this.setState({ disabled });
  }

  shouldComponentUpdate(nextProps) {
    const { componentName } = nextProps;
    let flag = true;
    if (nextProps.disabled !== this.state.disabled) {
      return true;
    }
    /* 弹窗数据回显需要刷新列表 */
    if (nextProps.refreshList) {
      return true;
    }
    if (nextProps.disabled === this.state.disabled && nextProps.disabled === false) {
      return false;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  /* 用于返回列表编辑框弹窗所属行主键 */
  handleRecordKey = (key) => {
    this.props.handleRecordKey({ workOrderJigSawDataKey: key });
  };

  render() {
    const { handleJigSawData } = this.props;
    const {
      app,
      form,
      dataExist,
      dataName,
      dataType,
      handleModalPane,
      dataList,
      dataTotal,
      clickCloumn,
      handleWorkOrderPaneChange,
    } = this.props;
    const {
      headerConfig,
      disabled,
      workOrderJigSawSlaveConfig,
      sActiveId,
    } = this.state;
    const staticEditProps = {
      sActiveId,
      showConfigList: workOrderJigSawSlaveConfig.gdsconfigformslave,
      headerConfig,
      dataListAll: dataExist ? dataList : [],
      disabled,
      handleTableData: handleJigSawData,
      handleRecordKey: this.handleRecordKey, /* 用于返回列表编辑框弹窗所属行主键 */
      app,
      form,
      dataName,
      dataType,
      handleModalPane,
      dataTotal, /* 列表汇总数据 */
      clickCloumn,
      handleWorkOrderPaneChange,
    };
    return (
      <FormItem className={styles.workerForm}>
        <Row type="flex" className={styles.workerFormRow}>
          <Col span={1}>
            <div className={styles.item}><br />拼<br />版</div>
          </Col>
          <Col span={23}>
            <StaticEditTable
              {...staticEditProps}
            />
          </Col>
        </Row>
      </FormItem>
    );
  }
}

export default JigsawComponent;
